import { create } from 'zustand';
import { devtools, persist, subscribeWithSelector } from 'zustand/middleware';
import { immer } from 'zustand/middleware/immer';

import { createSystemSlice } from './slices/system';
import { createUserSlice } from './slices/user';
import { createSettingsSlice } from './slices/settings';
import type { SystemSlice } from './slices/system';
import type { UserSlice } from './slices/user';
import type { SettingsSlice } from './slices/settings';

// 暂时简化的Store类型
export type SystemStore = SystemSlice & UserSlice & SettingsSlice;

// 创建store
export const useSystemStore = create<SystemStore>()(
  devtools(
    persist(
      subscribeWithSelector(
        immer((set, get, store) => ({
          ...createSystemSlice(set, get, store),
          ...createUserSlice(set, get, store),
          ...createSettingsSlice(set, get, store),
        }))
      ),
      {
        name: 'system-store',
        // 指定需要持久化的状态
        partialize: (state) => ({
          theme: state.theme,
          language: state.language,
          userInfo: state.userInfo,
          preferences: state.preferences,
          activeTab: state.activeTab,
        }),
        version: 1,
      }
    ),
    {
      name: 'SystemStore',
    }
  )
);

// 导出类型
export type { SystemSlice, UserSlice };
